package com.ruoyi.eqmonitor.controller;

import java.util.*;

import com.ruoyi.eqmonitor.domain.OptDevice;
import com.ruoyi.eqmonitor.service.IOptDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 后期加入的设备
 * @author ruoyi
 * @date 2023-03-01
 */
@RestController
@RequestMapping("/newDevice")
public class OptDeviceNewController extends BaseController{
    
    @Autowired
    private IOptDeviceService optDeviceNewService;

    /**
     * 查询设备管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<?> list(OptDevice optDeviceNew){
        startPage();
        List<OptDevice> list = optDeviceNewService.selectOptDeviceNewList(optDeviceNew);
        return getDataTable(list);
    }

    /**
     * 查询全部设备
     */
    @GetMapping("/all")
    public AjaxResult getAll(){
        return AjaxResult.success(optDeviceNewService.getAll());
    }

    /**
     * 根据id查询设备详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(optDeviceNewService.selectOptDeviceNewById(id));
    }

    /**
     * 新增设备
     */
    @Log(title = "新增设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptDevice optDeviceNew){
        return toAjax(optDeviceNewService.save(optDeviceNew));
    }

    /**
     * 修改设备信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OptDevice optDeviceNew){
        return toAjax(optDeviceNewService.updateById(optDeviceNew));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "删除设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(optDeviceNewService.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出设备列表
     */
    @Log(title = "导出设备列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptDevice optDeviceNew){
        List<OptDevice> list = optDeviceNewService.selectOptDeviceNewList(optDeviceNew);
        ExcelUtil<OptDevice> util = new ExcelUtil<OptDevice>(OptDevice.class);
        util.exportExcel(response, list, "设备数据");
    }

//    @ApiOperation("数据导入")
//    @PostMapping("/toLeadJSON")
//    public boolean toLeadJSON(HttpServletRequest request){
//        try {
//            MultipartHttpServletRequest requestStr1 = (MultipartHttpServletRequest) request;
//            Iterator<String> fileNames = requestStr1.getFileNames();
//            while (fileNames.hasNext()) {
//                String next = fileNames.next();
//                List<MultipartFile> files = requestStr1.getFiles(next);
//                if (files.size() > 0) {
//                    Iterator<MultipartFile> iterator = files.iterator();
//                    while (iterator.hasNext()) {
//                        MultipartFile next1 = iterator.next();//获取到文件
//                        InputStream inputStream = next1.getInputStream();
//                        return optDeviceNewService.toLeadJSON(inputStream);
//
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
    
}
